local biz_name = "cb"

ConfigRedisConnectors[biz_name]={
    host = "127.0.0.1", ---192.168.2.28
    port = 6379, --6379
    unixpath = "",
    db = NIL,
    user = NIL,
    passwd = NIL,
    queue = "queue_4"
}
parameter_index[biz_name] = {SIZE = 4, CLICK_PARAM_VALUES = 1, CLICK_URL_PARAM = 2, CLICK_REFERENCE_PARAM = 4}

local function cb()
      return coroutine.create(function ()
        while true do
            local DEF_LOCATION = "0"
            reply.setStatus(302);
            --reset cac cmd buffer
            luredis.clear_cmd_buffer()
            --parse header
            local headerTable = request.getHeaders_key_value()
            --parse cookie
            local cookieTable = parseCookies(getStringValue(headerTable, config.COOKIE_HEADER))
            --parse parameter
            local paraTable = request.parseParameters()--getParameters_value()
            --kiem tra xem so luong parameter trong uri co thoa man ko
            if #paraTable ~= parameter_index[biz_name].SIZE then break end

                --append server time to lua_State->l_G->redis_cmd_buf_append
                local createTime = os.time()
                luredis.append_cmd_buffer(createTime, config.SEPARATE_CHAR)
                --append client ip to lua_State->l_G->redis_cmd_buf_append
                local ip = request.getIP()
                luredis.append_cmd_buffer(ip, config.SEPARATE_CHAR)
                --append click or view = 1 --
                luredis.append_cmd_buffer(1, config.SEPARATE_CHAR)

                --Lay cac thong so
                --guid
                local guid = cookieTable[config.GUID_COOKIE]
                --region buffer
                local region = getStringValue(cookieTable, config.REGION_COOKIE)
                --os version buffer
                local osVersion = getStringValue(cookieTable, config.OS_MOBILE_COOKIE)
                --user agent
                local userAgent = getStringValue(headerTable, config.USER_AGENT_HEADER)
                --referer--
                local referer = getStringValue(headerTable, config.REFERER_HEADER)

            local cookieCreate = getStringValue(cookieTable,config.CREATE_COOKIE)

            local cookieDomain = ""
            if--if#7.1.1
                config.http.mobilecookie.domain and (config.http.mobilecookie.domain ~= "")
            then
                cookieDomain = config.http.mobilecookie.domain
            else
                cookieDomain = config.http.setcookie.domain
            end--end of if#7.1.1
            --kiem tra xem co region chua: cookie: __C
            --neu chua co vao hget lay, neu ko co, set default = 0. Set lai vao cookie
            local region_number = tonumber(region)
            if
                (not region_number) or (region_number <=3)
            then
                --get region from redis location
                local reply_type
                reply_type, region = util.redis_command(config.HGET_MOBILE_REDIS, false, luredis.send_cmd, "hget "..ConfigRedisConnectors[config.HGET_MOBILE_REDIS].queue.." "..c_inet_network(ip))
                if--if#7
                            (region == nil) or (region == "") or (reply_type == 6) --if error, reply_type = REDIS_REPLY_ERROR = 6
                then
                    region = DEF_LOCATION
                else
                    region_number = tonumber(region)
                    if--if#7.1
                        region_number and(region_number > 3)
                    then--
                        --Them region cookie
                        --Them create cookie
                        pushCookieHeader(config.REGION_COOKIE, region, os.time()+86400, "/", cookieDomain, false)
                    else
                        region = DEF_LOCATION
                    end--end of if#7.1
                end--end of if#7
            end
            --kiem tra xem co guid chua
            -- neu chua co tao guid moi, set cookie
            --create_cookie = guid create time
            if --if#2http://sohastore.vn/
                not guid
            then
                guid = create_guid(createTime, ip)
                if not guid then break end
                cookieCreate = createTime
                --set cookie
                --them cookie header vao _reply
                if--if#4
                        userAgent and userAgent ~= ""
                then
                        if--if#5
                                not(string.find(userAgent, "Safari")and (not string.find(userAgent, "Chrome")))
                        then
                                --Them guid cookie
                                --Them create cookie
                                pushCookieHeader(config.GUID_COOKIE, guid, config.EXPIRE_TIME+createTime, "/", cookieDomain, false)
                                pushCookieHeader(config.CREATE_COOKIE, createTime, config.EXPIRE_TIME+createTime, "/", cookieDomain, false)
                        end--end of if#5
                end--end of if#4
            end--end of if#2
            if string.len(osVersion) < 3
            then
                osVersion = parseUserAgent(userAgent)
                if osVersion then
                    pushCookieHeader(config.OS_MOBILE_COOKIE, osVersion, config.EXPIRE_TIME+createTime, "/", cookieDomain, false)
                end
            end
            if not osVersion then
                osVersion = ""
            end

            --append __uid, cookie create, __C
            luredis.append_cmd_buffer(guid, config.SEPARATE_CHAR, cookieCreate, config.SEPARATE_CHAR, region, config.SEPARATE_CHAR)

            --Append c;b;z;i;s;r;h;fe;je;sr;sc--
            luredis.append_cmd_buffer(getStringValue(paraTable,parameter_index[biz_name].CLICK_PARAM_VALUES), config.SEPARATE_CHAR)
            --append url--
            luredis.append_cmd_buffer(getStringValue(paraTable,parameter_index[biz_name].CLICK_URL_PARAM), config.SEPARATE_CHAR)
            --append referer, OS--
            luredis.append_cmd_buffer(referer, config.SEPARATE_CHAR, osVersion)


            --push data to redis--
            util.redis_command(biz_name, false, luredis.send_cmd_buffer, "lpush "..ConfigRedisConnectors[biz_name].queue.. " %s")
            --reference
            local http = getStringValue(paraTable,parameter_index[biz_name].CLICK_REFERENCE_PARAM)
            if string.len(http) > 3 then --if#10
                    if string.find(http, "://") then --if#10.1
                        if string.find(http, "|") then --if#10.1.1
                            --Thay dau | bang dau &
                            local newHttp = http:gsub("%|","&")
                            --push location header:
                            reply.pushHeader(config.LOCATION_HEADER, fast_url_decode(newHttp))
                        else    --else of if#10.1.1
                            reply.pushHeader(config.LOCATION_HEADER, fast_url_decode(http))
                        end --end of if#10.1.1

                    else --else of if#10.1
                        reply.pushHeader(config.LOCATION_HEADER, fast_url_decode(http))
                    end  --end of if#10.1
            else --else if#10
                --add header --
                --add header: httpServerName
                reply.pushHeader(config.SERVER_HEADER, config.http.servername)
                --add header: cache control header
                reply.pushHeader(config.CACHE_CONTROL_HEADER, "private, no-cache, no-cache=Set-Cookie, proxy-revalidate")
                --add header: pragma header
                reply.pushHeader(config.PRAGMA_HEADER, "no-cache")
                --add header: expire header
                reply.pushHeader(config.EXPIRES_HEADER, "Wed, 17 Sep 1975 21:32:10 GMT")
                --add header: content type header
                reply.pushHeader(config.CONTENT_TYPE_HEADER, "image/gif")
                --add header: last modify
                reply.pushHeader(config.LAST_MODIFIED_HEADER, "Wed, 19 Apr 2000 11:43:00 GMT")
                --add header: output
                reply.pushHeader("P3P", "CP=\"IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA\"")
            end--end if#10
            reply.pushHeader("Content-Length", "0")
                coroutine.yield()
            end--end of while 1
            f[biz_name] = cb()
----------------
        end)
    end
creators[biz_name] = function()
    f[biz_name] = cb()
end
